package com.wanmait.recruitment.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.recruitment.mapper.CommentMapper;
import com.wanmait.recruitment.pojo.Comment;
import com.wanmait.recruitment.service.CommentService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentMapper commentMapper;

    @Override
    //@Cacheable("commentCache")
    public PageInfo<Comment> findPage(Comment comment, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Comment> comments = commentMapper.selectListByJobId(comment);
        PageInfo<Comment> pageInfo = new PageInfo<>(comments);
        return pageInfo;

    }

    @Override
    //@Cacheable("commentCache")
    public List<Comment> selectListNextByUserinfoIds(List<Integer> str) {
        return commentMapper.selectListNextByUserinfoIds(str);
    }

    @Override
    public PageInfo<Comment> findAllPage(Comment comment, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Comment> comments = commentMapper.findAll(comment);
        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        return commentPageInfo;
    }

    @Override
    public List<Comment> selectListByParentId(Integer id) {
        return commentMapper.selectListByParentId(id);
    }

    @Override
    public void delReply(int id) {
        commentMapper.delReply(id);
    }

    @Override
    public void batchDelReply(String ids) {
        String idArr[] = ids.split(",");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < idArr.length; i++) {
           list.add(Integer.parseInt(idArr[i]));
        }
        commentMapper.batchDelReply(list);
    }

    @Override
    //@CacheEvict(value = "commentCache",allEntries = true)
    public void insert(Comment object) {
        commentMapper.insertSelective(object);
    }

    @Override
    public void delete(Integer id) {
        commentMapper.updateByPrimaryKey(id);
    }

    @Override
    public void delete(String ids) {
        String idArr[] = ids.split(",");
        List<Integer> list = new ArrayList<>();
        for (int i=0;i<idArr.length;i++){
            list.add(Integer.parseInt(idArr[i]));
        }
        commentMapper.updateByPrimaryKeySelective(list);
    }

    @Override
    public void update(Comment object) {

    }

    @Override
    public Comment findById(Integer id) {
        return null;
    }

    @Override
    public List<Comment> findAll() {
        return null;
    }

    @Override
    public List<Comment> findAll(Comment object) {
        return null;
    }

    @Override
    public PageInfo<Comment> findPage(int pageNum, int pageSize) {
        return null;
    }
}
